/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.mb;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import net.easoftware.code.beans.ComboItem;
import net.easoftware.code.beans.facade.LineaNegocio;
import net.easoftware.code.dao.CatalogosDao;
import net.easoftware.code.dao.PersistenceEjb;
import net.easoftware.code.error.EaError;
import net.easoftware.code.mb.annotation.GenericMaintenanceConfig;
import net.easoftware.code.util.Tools;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author eliu
 */
@SessionScoped
@Named
@GenericMaintenanceConfig(actions = {GenericMaintenanceConfig.NEW, GenericMaintenanceConfig.DEL}, title = "Mantenimiento de Lineas de Negocio")
public class MbLineasNegocio implements Serializable, GenericMaintenanceWrapper {

    private final Logger log = Logger.getLogger(MbGenericMaintenance.class);
    private List<ComboItem> estados = new LinkedList<>();
    private List<LineaNegocio> lista = new LinkedList<>();
    @Inject
    private LineaNegocio bean;
    @Inject
    CatalogosDao dao;
    @EJB
    PersistenceEjb ejb;
    private LineaNegocio selected;

    public LineaNegocio getSelected() {
        return selected;
    }

    public void setSelected(LineaNegocio selected) {
        this.selected = selected;
    }

    public List<ComboItem> getEstados() {
        if (estados.isEmpty()) {
            estados = Tools.getEstados();
        }
        return estados;
    }

    public void setEstados(List<ComboItem> estados) {
        this.estados = estados;
    }

    public List<LineaNegocio> getLista() {
        if (lista == null || lista.isEmpty()) {
            try {
                lista = dao.getAllLineasNegocio();
            }
            catch (EaError ex) {
                java.util.logging.Logger.getLogger(MbLineasNegocio.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return lista;
    }

    public void setLista(List<LineaNegocio> rangos) {
        this.lista = rangos;
    }

    @Override
    public void actionSave() throws EaError {
        ejb.guardar(bean.getEntityBean());
        lista = null;
    }

    @Override
    public void actionUpdate() throws EaError {

        ejb.actualizar(bean.getEntityBean());
        lista = null;
    }

    @Override
    public void actionDelete() throws EaError {
        ejb.eliminar(bean.getEntityBean());
        lista = null;
    }

    @Override
    public void actionNew() throws EaError {
        bean.setNombre("");
    }

    @Override
    public void toExcel() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void toPDF() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getFormPage() {
        return "/module/lineas/linea.xhtml";
    }

    @Override
    public String getGridPage() {
        return "/module/lineas/lineas.xhtml";
    }

    @Override
    public void actionSelect() throws EaError {
        try {
            BeanUtils.copyProperties(this.bean, this.selected);
        }
        catch (IllegalAccessException | InvocationTargetException ex) {
            throw new EaError("Error mostrando datos objeto!", ex);
        }
    }

    @Override
    public void actionCustom1() throws EaError {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void actionCustom2() throws EaError {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getActionCustom1Label() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getActionCustom2Label() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isEnabledActionCustom1() {
        return false;
    }

    @Override
    public boolean isEnabledActionCustom2() {
        return false;
    }
}
